﻿//the javascript to respond to the master page event must be available on each content page (cannot place it on master page)
//note that the divDialogConfig section is defined on the Master page, and this .js file must be included on each content page like this:
//    <script src="Config.js"></script>

//as of 2/26/13 this code is no longer needed; configuration moved to a dedicated web page

var lastProject, lastTable

function showConfigDialog() {

    //retrieve the previously selected project and table so can be set a defaults

    PageMethods.GetProject_Table(function (arProjTbl) {
        lastProject = arProjTbl[0];
        lastTable = arProjTbl[1];

        //fill project list using PageMethod (which must be available in content page codebehind)

        PageMethods.GetProjectList(function (arProj) {
            for (var i = 0; i < arProj.length; i++) {
                $("#cboProjects").append("<option value='" + arProj[i] + "'>" + arProj[i] + "</option>");
            };

            //set the default project

            $("#cboProjects").val(lastProject);

            //add the tables in that project
            //(instead of calling fillTables function, must include call to async page method here so everything is done before dialog is displayed)

            PageMethods.GetTableList($("#cboProjects").val(), function (arTbl) {
                for (var i = 0; i < arTbl.length; i++) {
                    $("#lstTables").append("<option value='" + arTbl[i] + "'>" + arTbl[i] + "</option>");
                };
                $("#lstTables").val(lastTable);

                //display the dialog

                $("#divDialogConfig").dialog({
                    modal: true,
                    width: 400,
                    height: 400,
                    buttons: {

                        OK: function () {
                            //4. when popup is closed, call pagemethod to reset project and table
                            PageMethods.SetProject_Table($("#cboProjects").val(), $("#lstTables").val(), function () {
                                //set button text to new values
                                $("#Config").val("Current Settings: " + $("#cboProjects").val() + ": " + $("#lstTables").val());
                            });

                            $(this).dialog("close");
                        },

                        Cancel: function () {
                            $(this).dialog("close");
                        }
                    }
                })
            });
        });
    });

}

function fillTableList() {
    //fill project list using PageMethod (which must be available in content page codebehind)
    //note: this code must be called in response to selected project getting changed
    PageMethods.GetTableList($("#cboProjects").val(), function (arTbl) {
        for (var i = 0; i < arTbl.length; i++) {
            $("#lstTables").append("<option value='" + arTbl[i] + "'>" + arTbl[i] + "</option>");
        };
    });
}

